package com.jin.crud.controller;


import com.github.pagehelper.PageInfo;
import com.jin.crud.bean.QueryDto;
import com.jin.crud.bean.SysRole;
import com.jin.crud.bean.SysUser;
import com.jin.crud.common.utils.ResultUtil;
import com.jin.crud.common.utils.StringUtils;
import com.jin.crud.service.SysRoleService;
import com.jin.crud.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author yuxuejin
 * @since 2021-11-16
 */
@Api("用户管理")
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    SysUserService userService;

    @Autowired
    SysRoleService roleService;

    @ApiOperation("分页查询用户")
    @GetMapping("/getAllPage")
    public ResultUtil<PageInfo<SysUser>> getAllPage(QueryDto queryDto) {
        return ResultUtil.resultSuccess(userService.getAllPage(queryDto));
    }

    @ApiOperation("查询用户详细信息")
    @GetMapping("/getUser")
    public ResultUtil<SysUser> getUser(@RequestParam(value = "userId", required = false) String userId) {
        List<SysRole> roles = roleService.getAllRole();
        Map<String, Object> map = new HashMap<>();
        map.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        if (StringUtils.isNotNull(userId)) {
            map.put("user", userService.selectUserById(userId));
            map.put("roleIds", roleService.selectRoleListByUserId(userId));
        }
        return ResultUtil.resultSuccess(map);
    }

    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public ResultUtil addUser(SysUser user) {
        userService.addUser(user);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateUser")
    public ResultUtil updateUser(SysUser user) {
        userService.updateUser(user);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("删除用户")
    @PostMapping("/delUser")
    public ResultUtil delUser(@RequestParam("userId") String userId) {
        userService.delUser(userId);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("修改用户状态")
    @PostMapping("/updateUserStatus")
    public ResultUtil updateUserStatus(@RequestParam("userId") String userId, @RequestParam("status") String status) {
        userService.updateUserStatus(userId, status);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("重置密码")
    @PostMapping("/resetUserPwd")
    public ResultUtil resetPwd(String userId) {
        userService.checkUserAllowed(userId);
        userService.resetPwd(userId);
        return ResultUtil.resultSuccess();
    }

    @ApiOperation("根据用户编号获取授权角色")
    @GetMapping("/getAuthRole")
    public ResultUtil getAuthRole(@RequestParam("userId") String userId) {
        return ResultUtil.resultSuccess(userService.getAuthRole(userId));
    }

    @ApiOperation("用户授权角色")
    @PostMapping("updateAuthRole")
    public ResultUtil updateAuthRole(String userId, String[] roleIds) {
        userService.insertUserAuth(userId, roleIds);
        return ResultUtil.resultSuccess();
    }

}

